home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / valarith.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  27.9 KB  |  1,044 lines

  1. /* Perform arithmetic and other operations on values, for GDB.
  2.    Copyright 1986, 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "value.h"
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "expression.h"
  25. #include "target.h"
  26. #include "language.h"
  27. #include "demangle.h"
  28. #include <string.h>
  29.  
  30. /* Define whether or not the C operator '/' truncates towards zero for
  31.    differently signed operands (truncation direction is undefined in C). */
  32.  
  33. #ifndef TRUNCATION_TOWARDS_ZERO
  34. #define TRUNCATION_TOWARDS_ZERO ((-5 / 2) == -2)
  35. #endif
  36.  
  37. static value
  38. value_subscripted_rvalue PARAMS ((value, value));
  39.  
  40.  
  41. value
  42. value_add (arg1, arg2)
  43.     value arg1, arg2;
  44. {
  45.   register value valint, valptr;
  46.   register int len;
  47.  
  48.   COERCE_ARRAY (arg1);
  49.   COERCE_ARRAY (arg2);
  50.  
  51.   if ((TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
  52.        || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_PTR)
  53.       &&
  54.       (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT
  55.        || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT))
  56.     /* Exactly one argument is a pointer, and one is an integer.  */
  57.     {
  58.       if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  59.     {
  60.       valptr = arg1;
  61.       valint = arg2;
  62.     }
  63.       else
  64.     {
  65.       valptr = arg2;
  66.       valint = arg1;
  67.     }
  68.       len = TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (valptr)));
  69.       if (len == 0) len = 1;    /* For (void *) */
  70.       return value_from_longest (VALUE_TYPE (valptr),
  71.                   value_as_long (valptr)
  72.                   + (len * value_as_long (valint)));
  73.     }
  74.  
  75.   return value_binop (arg1, arg2, BINOP_ADD);
  76. }
  77.  
  78. value
  79. value_sub (arg1, arg2)
  80.     value arg1, arg2;
  81. {
  82.  
  83.   COERCE_ARRAY (arg1);
  84.   COERCE_ARRAY (arg2);
  85.  
  86.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  87.     {
  88.       if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
  89.     {
  90.       /* pointer - integer.  */
  91.       return value_from_longest
  92.         (VALUE_TYPE (arg1),
  93.          value_as_long (arg1)
  94.          - (TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)))
  95.         * value_as_long (arg2)));
  96.     }
  97.       else if (VALUE_TYPE (arg1) == VALUE_TYPE (arg2))
  98.     {
  99.       /* pointer to <type x> - pointer to <type x>.  */
  100.       return value_from_longest
  101.         (builtin_type_long,        /* FIXME -- should be ptrdiff_t */
  102.          (value_as_long (arg1) - value_as_long (arg2))
  103.          / TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))));
  104.     }
  105.       else
  106.     {
  107.       error ("\
  108. First argument of `-' is a pointer and second argument is neither\n\
  109. an integer nor a pointer of the same type.");
  110.     }
  111.     }
  112.  
  113.   return value_binop (arg1, arg2, BINOP_SUB);
  114. }
  115.  
  116. /* Return the value of ARRAY[IDX].
  117.    See comments in value_coerce_array() for rationale for reason for
  118.    doing lower bounds adjustment here rather than there.
  119.    FIXME:  Perhaps we should validate that the index is valid and if
  120.    verbosity is set, warn about invalid indices (but still use them). */
  121.  
  122. value
  123. value_subscript (array, idx)
  124.      value array, idx;
  125. {
  126.   int lowerbound;
  127.   value bound;
  128.   struct type *range_type;
  129.  
  130.   COERCE_REF (array);
  131.  
  132.   if (TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_ARRAY
  133.       || TYPE_CODE (VALUE_TYPE (array)) == TYPE_CODE_STRING)
  134.     {
  135.       range_type = TYPE_FIELD_TYPE (VALUE_TYPE (array), 0);
  136.       lowerbound = TYPE_FIELD_BITPOS (range_type, 0);
  137.       if (lowerbound != 0)
  138.     {
  139.       bound = value_from_longest (builtin_type_int, (LONGEST) lowerbound);
  140.       idx = value_sub (idx, bound);
  141.     }
  142.       if (VALUE_LVAL (array) != lval_memory)
  143.     {
  144.       return value_subscripted_rvalue (array, idx);
  145.     }
  146.       array = value_coerce_array (array);
  147.     }
  148.   return value_ind (value_add (array, idx));
  149. }
  150.  
  151. /* Return the value of EXPR[IDX], expr an aggregate rvalue
  152.    (eg, a vector register).  This routine used to promote floats
  153.    to doubles, but no longer does.  */
  154.  
  155. static value
  156. value_subscripted_rvalue (array, idx)
  157.      value array, idx;
  158. {
  159.   struct type *elt_type = TYPE_TARGET_TYPE (VALUE_TYPE (array));
  160.   int elt_size = TYPE_LENGTH (elt_type);
  161.   int elt_offs = elt_size * longest_to_int (value_as_long (idx));
  162.   value v;
  163.  
  164.   if (elt_offs >= TYPE_LENGTH (VALUE_TYPE (array)))
  165.     error ("no such vector element");
  166.  
  167.   v = allocate_value (elt_type);
  168.   memcpy (VALUE_CONTENTS (v), VALUE_CONTENTS (array) + elt_offs, elt_size);
  169.  
  170.   if (VALUE_LVAL (array) == lval_internalvar)
  171.     VALUE_LVAL (v) = lval_internalvar_component;
  172.   else
  173.     VALUE_LVAL (v) = not_lval;
  174.   VALUE_ADDRESS (v) = VALUE_ADDRESS (array);
  175.   VALUE_OFFSET (v) = VALUE_OFFSET (array) + elt_offs;
  176.   VALUE_BITSIZE (v) = elt_size * 8;
  177.   return v;
  178. }
  179.  
  180. /* Check to see if either argument is a structure.  This is called so
  181.    we know whether to go ahead with the normal binop or look for a 
  182.    user defined function instead.
  183.  
  184.    For now, we do not overload the `=' operator.  */
  185.  
  186. int
  187. binop_user_defined_p (op, arg1, arg2)
  188.      enum exp_opcode op;
  189.      value arg1, arg2;
  190. {
  191.   if (op == BINOP_ASSIGN)
  192.     return 0;
  193.   return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
  194.       || TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_STRUCT
  195.       || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
  196.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT)
  197.       || (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_REF
  198.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) == TYPE_CODE_STRUCT));
  199. }
  200.  
  201. /* Check to see if argument is a structure.  This is called so
  202.    we know whether to go ahead with the normal unop or look for a 
  203.    user defined function instead.
  204.  
  205.    For now, we do not overload the `&' operator.  */
  206.  
  207. int unop_user_defined_p (op, arg1)
  208.      enum exp_opcode op;
  209.      value arg1;
  210. {
  211.   if (op == UNOP_ADDR)
  212.     return 0;
  213.   return (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT
  214.       || (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
  215.           && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1))) == TYPE_CODE_STRUCT));
  216. }
  217.  
  218. /* We know either arg1 or arg2 is a structure, so try to find the right
  219.    user defined function.  Create an argument vector that calls 
  220.    arg1.operator @ (arg1,arg2) and return that value (where '@' is any
  221.    binary operator which is legal for GNU C++).
  222.  
  223.    OP is the operatore, and if it is BINOP_ASSIGN_MODIFY, then OTHEROP
  224.    is the opcode saying how to modify it.  Otherwise, OTHEROP is
  225.    unused.  */
  226.  
  227. value
  228. value_x_binop (arg1, arg2, op, otherop)
  229.      value arg1, arg2;
  230.      enum exp_opcode op, otherop;
  231. {
  232.   value * argvec;
  233.   char *ptr, *mangle_ptr;
  234.   char tstr[13], mangle_tstr[13];
  235.   int static_memfuncp;
  236.  
  237.   COERCE_REF (arg1);
  238.   COERCE_REF (arg2);
  239.   COERCE_ENUM (arg1);
  240.   COERCE_ENUM (arg2);
  241.  
  242.   /* now we know that what we have to do is construct our
  243.      arg vector and find the right function to call it with.  */
  244.  
  245.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
  246.     error ("Can't do that binary op on that type");  /* FIXME be explicit */
  247.  
  248.   argvec = (value *) alloca (sizeof (value) * 4);
  249.   argvec[1] = value_addr (arg1);
  250.   argvec[2] = arg2;
  251.   argvec[3] = 0;
  252.  
  253.   /* make the right function name up */  
  254.   strcpy(tstr, "operator__");
  255.   ptr = tstr+8;
  256.   switch (op)
  257.     {
  258.     case BINOP_ADD:        strcpy(ptr,"+"); break;
  259.     case BINOP_SUB:        strcpy(ptr,"-"); break;
  260.     case BINOP_MUL:        strcpy(ptr,"*"); break;
  261.     case BINOP_DIV:        strcpy(ptr,"/"); break;
  262.     case BINOP_REM:        strcpy(ptr,"%"); break;
  263.     case BINOP_LSH:        strcpy(ptr,"<<"); break;
  264.     case BINOP_RSH:        strcpy(ptr,">>"); break;
  265.     case BINOP_BITWISE_AND:    strcpy(ptr,"&"); break;
  266.     case BINOP_BITWISE_IOR:    strcpy(ptr,"|"); break;
  267.     case BINOP_BITWISE_XOR:    strcpy(ptr,"^"); break;
  268.     case BINOP_LOGICAL_AND:    strcpy(ptr,"&&"); break;
  269.     case BINOP_LOGICAL_OR:    strcpy(ptr,"||"); break;
  270.     case BINOP_MIN:        strcpy(ptr,"<?"); break;
  271.     case BINOP_MAX:        strcpy(ptr,">?"); break;
  272.     case BINOP_ASSIGN:        strcpy(ptr,"="); break;
  273.     case BINOP_ASSIGN_MODIFY:    
  274.       switch (otherop)
  275.     {
  276.     case BINOP_ADD:        strcpy(ptr,"+="); break;
  277.     case BINOP_SUB:        strcpy(ptr,"-="); break;
  278.     case BINOP_MUL:        strcpy(ptr,"*="); break;
  279.     case BINOP_DIV:        strcpy(ptr,"/="); break;
  280.     case BINOP_REM:        strcpy(ptr,"%="); break;
  281.     case BINOP_BITWISE_AND:    strcpy(ptr,"&="); break;
  282.     case BINOP_BITWISE_IOR:    strcpy(ptr,"|="); break;
  283.     case BINOP_BITWISE_XOR:    strcpy(ptr,"^="); break;
  284.     case BINOP_MOD:        /* invalid */
  285.     default:
  286.       error ("Invalid binary operation specified.");
  287.     }
  288.       break;
  289.     case BINOP_SUBSCRIPT: strcpy(ptr,"[]"); break;
  290.     case BINOP_EQUAL:      strcpy(ptr,"=="); break;
  291.     case BINOP_NOTEQUAL:  strcpy(ptr,"!="); break;
  292.     case BINOP_LESS:      strcpy(ptr,"<"); break;
  293.     case BINOP_GTR:       strcpy(ptr,">"); break;
  294.     case BINOP_GEQ:       strcpy(ptr,">="); break;
  295.     case BINOP_LEQ:       strcpy(ptr,"<="); break;
  296.     case BINOP_MOD:      /* invalid */
  297.     default:
  298.       error ("Invalid binary operation specified.");
  299.     }
  300.  
  301.   argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
  302.   
  303.   if (argvec[0])
  304.     {
  305.       if (static_memfuncp)
  306.     {
  307.       argvec[1] = argvec[0];
  308.       argvec++;
  309.     }
  310.       return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
  311.     }
  312.   error ("member function %s not found", tstr);
  313. #ifdef lint
  314.   return call_function_by_hand (argvec[0], 2 - static_memfuncp, argvec + 1);
  315. #endif
  316. }
  317.  
  318. /* We know that arg1 is a structure, so try to find a unary user
  319.    defined operator that matches the operator in question.  
  320.    Create an argument vector that calls arg1.operator @ (arg1)
  321.    and return that value (where '@' is (almost) any unary operator which
  322.    is legal for GNU C++).  */
  323.  
  324. value
  325. value_x_unop (arg1, op)
  326.      value arg1;
  327.      enum exp_opcode op;
  328. {
  329.   value * argvec;
  330.   char *ptr, *mangle_ptr;
  331.   char tstr[13], mangle_tstr[13];
  332.   int static_memfuncp;
  333.  
  334.   COERCE_ENUM (arg1);
  335.  
  336.   /* now we know that what we have to do is construct our
  337.      arg vector and find the right function to call it with.  */
  338.  
  339.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_STRUCT)
  340.     error ("Can't do that unary op on that type");  /* FIXME be explicit */
  341.  
  342.   argvec = (value *) alloca (sizeof (value) * 3);
  343.   argvec[1] = value_addr (arg1);
  344.   argvec[2] = 0;
  345.  
  346.   /* make the right function name up */  
  347.   strcpy(tstr,"operator__");
  348.   ptr = tstr+8;
  349.   strcpy(mangle_tstr, "__");
  350.   mangle_ptr = mangle_tstr+2;
  351.   switch (op)
  352.     {
  353.     case UNOP_PREINCREMENT:    strcpy(ptr,"++"); break;
  354.     case UNOP_PREDECREMENT:    strcpy(ptr,"++"); break;
  355.     case UNOP_POSTINCREMENT:    strcpy(ptr,"++"); break;
  356.     case UNOP_POSTDECREMENT:    strcpy(ptr,"++"); break;
  357.     case UNOP_LOGICAL_NOT:    strcpy(ptr,"!"); break;
  358.     case UNOP_COMPLEMENT:    strcpy(ptr,"~"); break;
  359.     case UNOP_NEG:        strcpy(ptr,"-"); break;
  360.     default:
  361.       error ("Invalid binary operation specified.");
  362.     }
  363.  
  364.   argvec[0] = value_struct_elt (&arg1, argvec+1, tstr, &static_memfuncp, "structure");
  365.  
  366.   if (argvec[0])
  367.     {
  368.       if (static_memfuncp)
  369.     {
  370.       argvec[1] = argvec[0];
  371.       argvec++;
  372.     }
  373.       return call_function_by_hand (argvec[0], 1 - static_memfuncp, argvec + 1);
  374.     }
  375.   error ("member function %s not found", tstr);
  376.   return 0;  /* For lint -- never reached */
  377. }
  378.  
  379.  
  380. /* Concatenate two values with the following conditions:
  381.  
  382.    (1)    Both values must be either bitstring values or character string
  383.     values and the resulting value consists of the concatenation of
  384.     ARG1 followed by ARG2.
  385.  
  386.     or
  387.  
  388.     One value must be an integer value and the other value must be
  389.     either a bitstring value or character string value, which is
  390.     to be repeated by the number of times specified by the integer
  391.     value.
  392.  
  393.  
  394.     (2)    Boolean values are also allowed and are treated as bit string
  395.         values of length 1.
  396.  
  397.     (3)    Character values are also allowed and are treated as character
  398.         string values of length 1.
  399. */
  400.  
  401. value
  402. value_concat (arg1, arg2)
  403.      value arg1, arg2;
  404. {
  405.   register value inval1, inval2, outval;
  406.   int inval1len, inval2len;
  407.   int count, idx;
  408.   char *ptr;
  409.   char inchar;
  410.  
  411.   /* First figure out if we are dealing with two values to be concatenated
  412.      or a repeat count and a value to be repeated.  INVAL1 is set to the
  413.      first of two concatenated values, or the repeat count.  INVAL2 is set
  414.      to the second of the two concatenated values or the value to be 
  415.      repeated. */
  416.  
  417.   if (TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_INT)
  418.     {
  419.       inval1 = arg2;
  420.       inval2 = arg1;
  421.     }
  422.   else
  423.     {
  424.       inval1 = arg1;
  425.       inval2 = arg2;
  426.     }
  427.  
  428.   /* Now process the input values. */
  429.  
  430.   if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_INT)
  431.     {
  432.       /* We have a repeat count.  Validate the second value and then
  433.      construct a value repeated that many times. */
  434.       if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_STRING
  435.       || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
  436.     {
  437.       count = longest_to_int (value_as_long (inval1));
  438.       inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
  439.       ptr = (char *) alloca (count * inval2len);
  440.       if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
  441.         {
  442.           inchar = (char) unpack_long (VALUE_TYPE (inval2),
  443.                        VALUE_CONTENTS (inval2));
  444.           for (idx = 0; idx < count; idx++)
  445.         {
  446.           *(ptr + idx) = inchar;
  447.         }
  448.         }
  449.       else
  450.         {
  451.           for (idx = 0; idx < count; idx++)
  452.         {
  453.           memcpy (ptr + (idx * inval2len), VALUE_CONTENTS (inval2),
  454.               inval2len);
  455.         }
  456.         }
  457.       outval = value_string (ptr, count * inval2len);
  458.     }
  459.       else if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BITSTRING
  460.            || TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_BOOL)
  461.     {
  462.       error ("unimplemented support for bitstring/boolean repeats");
  463.     }
  464.       else
  465.     {
  466.       error ("can't repeat values of that type");
  467.     }
  468.     }
  469.   else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_STRING
  470.       || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
  471.     {
  472.       /* We have two character strings to concatenate. */
  473.       if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_STRING
  474.       && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_CHAR)
  475.     {
  476.       error ("Strings can only be concatenated with other strings.");
  477.     }
  478.       inval1len = TYPE_LENGTH (VALUE_TYPE (inval1));
  479.       inval2len = TYPE_LENGTH (VALUE_TYPE (inval2));
  480.       ptr = (char *) alloca (inval1len + inval2len);
  481.       if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_CHAR)
  482.     {
  483.       *ptr = (char) unpack_long (VALUE_TYPE (inval1), VALUE_CONTENTS (inval1));
  484.     }
  485.       else
  486.     {
  487.       memcpy (ptr, VALUE_CONTENTS (inval1), inval1len);
  488.     }
  489.       if (TYPE_CODE (VALUE_TYPE (inval2)) == TYPE_CODE_CHAR)
  490.     {
  491.       *(ptr + inval1len) = 
  492.         (char) unpack_long (VALUE_TYPE (inval2), VALUE_CONTENTS (inval2));
  493.     }
  494.       else
  495.     {
  496.       memcpy (ptr + inval1len, VALUE_CONTENTS (inval2), inval2len);
  497.     }
  498.       outval = value_string (ptr, inval1len + inval2len);
  499.     }
  500.   else if (TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BITSTRING
  501.        || TYPE_CODE (VALUE_TYPE (inval1)) == TYPE_CODE_BOOL)
  502.     {
  503.       /* We have two bitstrings to concatenate. */
  504.       if (TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BITSTRING
  505.       && TYPE_CODE (VALUE_TYPE (inval2)) != TYPE_CODE_BOOL)
  506.     {
  507.       error ("Bitstrings or booleans can only be concatenated with other bitstrings or booleans.");
  508.     }
  509.       error ("unimplemented support for bitstring/boolean concatenation.");
  510.     }      
  511.   else
  512.     {
  513.       /* We don't know how to concatenate these operands. */
  514.       error ("illegal operands for concatenation.");
  515.     }
  516.   return (outval);
  517. }
  518.  
  519.  
  520. /* Perform a binary operation on two operands which have reasonable
  521.    representations as integers or floats.  This includes booleans,
  522.    characters, integers, or floats.
  523.    Does not support addition and subtraction on pointers;
  524.    use value_add or value_sub if you want to handle those possibilities.  */
  525.  
  526. value
  527. value_binop (arg1, arg2, op)
  528.      value arg1, arg2;
  529.      enum exp_opcode op;
  530. {
  531.   register value val;
  532.  
  533.   COERCE_ENUM (arg1);
  534.   COERCE_ENUM (arg2);
  535.  
  536.   if ((TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_FLT
  537.        &&
  538.        TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_CHAR
  539.        &&
  540.        TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT
  541.        &&
  542.        TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_BOOL)
  543.       ||
  544.       (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_FLT
  545.        &&
  546.        TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_CHAR
  547.        &&
  548.        TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT
  549.        &&
  550.        TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_BOOL))
  551.     error ("Argument to arithmetic operation not a number or boolean.");
  552.  
  553.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT
  554.       ||
  555.       TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_FLT)
  556.     {
  557.       /* FIXME-if-picky-about-floating-accuracy: Should be doing this
  558.      in target format.  real.c in GCC probably has the necessary
  559.      code.  */
  560.       double v1, v2, v;
  561.       v1 = value_as_double (arg1);
  562.       v2 = value_as_double (arg2);
  563.       switch (op)
  564.     {
  565.     case BINOP_ADD:
  566.       v = v1 + v2;
  567.       break;
  568.  
  569.     case BINOP_SUB:
  570.       v = v1 - v2;
  571.       break;
  572.  
  573.     case BINOP_MUL:
  574.       v = v1 * v2;
  575.       break;
  576.  
  577.     case BINOP_DIV:
  578.       v = v1 / v2;
  579.       break;
  580.  
  581.     default:
  582.       error ("Integer-only operation on floating point number.");
  583.     }
  584.  
  585.       val = allocate_value (builtin_type_double);
  586.       store_floating (VALUE_CONTENTS_RAW (val), TYPE_LENGTH (VALUE_TYPE (val)),
  587.               v);
  588.     }
  589.   else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_BOOL
  590.        &&
  591.        TYPE_CODE (VALUE_TYPE (arg2)) == TYPE_CODE_BOOL)
  592.       {
  593.       LONGEST v1, v2, v;
  594.       v1 = value_as_long (arg1);
  595.       v2 = value_as_long (arg2);
  596.       
  597.       switch (op)
  598.         {
  599.         case BINOP_BITWISE_AND:
  600.           v = v1 & v2;
  601.           break;
  602.           
  603.         case BINOP_BITWISE_IOR:
  604.           v = v1 | v2;
  605.           break;
  606.           
  607.         case BINOP_BITWISE_XOR:
  608.           v = v1 ^ v2;
  609.           break;
  610.           
  611.         default:
  612.           error ("Invalid operation on booleans.");
  613.         }
  614.       
  615.       val = allocate_value (builtin_type_chill_bool);
  616.       store_signed_integer (VALUE_CONTENTS_RAW (val),
  617.                 TYPE_LENGTH (VALUE_TYPE (val)),
  618.                 v);
  619.       }
  620.   else
  621.     /* Integral operations here.  */
  622.     /* FIXME:  Also mixed integral/booleans, with result an integer. */
  623.     {
  624.       /* Should we promote to unsigned longest?  */
  625.       if ((TYPE_UNSIGNED (VALUE_TYPE (arg1))
  626.        || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
  627.       && (TYPE_LENGTH (VALUE_TYPE (arg1)) >= sizeof (unsigned LONGEST)
  628.           || TYPE_LENGTH (VALUE_TYPE (arg2)) >= sizeof (unsigned LONGEST)))
  629.     {
  630.       unsigned LONGEST v1, v2, v;
  631.       v1 = (unsigned LONGEST) value_as_long (arg1);
  632.       v2 = (unsigned LONGEST) value_as_long (arg2);
  633.       
  634.       switch (op)
  635.         {
  636.         case BINOP_ADD:
  637.           v = v1 + v2;
  638.           break;
  639.           
  640.         case BINOP_SUB:
  641.           v = v1 - v2;
  642.           break;
  643.           
  644.         case BINOP_MUL:
  645.           v = v1 * v2;
  646.           break;
  647.           
  648.         case BINOP_DIV:
  649.           v = v1 / v2;
  650.           break;
  651.           
  652.         case BINOP_REM:
  653.           v = v1 % v2;
  654.           break;
  655.           
  656.         case BINOP_MOD:
  657.           /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
  658.              v1 mod 0 has a defined value, v1. */
  659.           /* Chill specifies that v2 must be > 0, so check for that. */
  660.           if (current_language -> la_language == language_chill
  661.           && value_as_long (arg2) <= 0)
  662.         {
  663.           error ("Second operand of MOD must be greater than zero.");
  664.         }
  665.           if (v2 == 0)
  666.         {
  667.           v = v1;
  668.         }
  669.           else
  670.         {
  671.           v = v1/v2;
  672.           /* Note floor(v1/v2) == v1/v2 for unsigned. */
  673.           v = v1 - (v2 * v);
  674.         }
  675.           break;
  676.           
  677.         case BINOP_LSH:
  678.           v = v1 << v2;
  679.           break;
  680.           
  681.         case BINOP_RSH:
  682.           v = v1 >> v2;
  683.           break;
  684.           
  685.         case BINOP_BITWISE_AND:
  686.           v = v1 & v2;
  687.           break;
  688.           
  689.         case BINOP_BITWISE_IOR:
  690.           v = v1 | v2;
  691.           break;
  692.           
  693.         case BINOP_BITWISE_XOR:
  694.           v = v1 ^ v2;
  695.           break;
  696.           
  697.         case BINOP_LOGICAL_AND:
  698.           v = v1 && v2;
  699.           break;
  700.           
  701.         case BINOP_LOGICAL_OR:
  702.           v = v1 || v2;
  703.           break;
  704.           
  705.         case BINOP_MIN:
  706.           v = v1 < v2 ? v1 : v2;
  707.           break;
  708.           
  709.         case BINOP_MAX:
  710.           v = v1 > v2 ? v1 : v2;
  711.           break;
  712.           
  713.         default:
  714.           error ("Invalid binary operation on numbers.");
  715.         }
  716.  
  717.       val = allocate_value (BUILTIN_TYPE_UNSIGNED_LONGEST);
  718.       store_unsigned_integer (VALUE_CONTENTS_RAW (val),
  719.                   TYPE_LENGTH (VALUE_TYPE (val)),
  720.                   v);
  721.     }
  722.       else
  723.     {
  724.       LONGEST v1, v2, v;
  725.       v1 = value_as_long (arg1);
  726.       v2 = value_as_long (arg2);
  727.       
  728.       switch (op)
  729.         {
  730.         case BINOP_ADD:
  731.           v = v1 + v2;
  732.           break;
  733.           
  734.         case BINOP_SUB:
  735.           v = v1 - v2;
  736.           break;
  737.           
  738.         case BINOP_MUL:
  739.           v = v1 * v2;
  740.           break;
  741.           
  742.         case BINOP_DIV:
  743.           v = v1 / v2;
  744.           break;
  745.           
  746.         case BINOP_REM:
  747.           v = v1 % v2;
  748.           break;
  749.           
  750.         case BINOP_MOD:
  751.           /* Knuth 1.2.4, integer only.  Note that unlike the C '%' op,
  752.              X mod 0 has a defined value, X. */
  753.           /* Chill specifies that v2 must be > 0, so check for that. */
  754.           if (current_language -> la_language == language_chill
  755.           && v2 <= 0)
  756.         {
  757.           error ("Second operand of MOD must be greater than zero.");
  758.         }
  759.           if (v2 == 0)
  760.         {
  761.           v = v1;
  762.         }
  763.           else
  764.         {
  765.           v = v1/v2;
  766.           /* Compute floor. */
  767.           if (TRUNCATION_TOWARDS_ZERO && (v < 0) && ((v1 % v2) != 0))
  768.             {
  769.               v--;
  770.             }
  771.           v = v1 - (v2 * v);
  772.         }
  773.           break;
  774.           
  775.         case BINOP_LSH:
  776.           v = v1 << v2;
  777.           break;
  778.           
  779.         case BINOP_RSH:
  780.           v = v1 >> v2;
  781.           break;
  782.           
  783.         case BINOP_BITWISE_AND:
  784.           v = v1 & v2;
  785.           break;
  786.           
  787.         case BINOP_BITWISE_IOR:
  788.           v = v1 | v2;
  789.           break;
  790.           
  791.         case BINOP_BITWISE_XOR:
  792.           v = v1 ^ v2;
  793.           break;
  794.           
  795.         case BINOP_LOGICAL_AND:
  796.           v = v1 && v2;
  797.           break;
  798.           
  799.         case BINOP_LOGICAL_OR:
  800.           v = v1 || v2;
  801.           break;
  802.           
  803.         case BINOP_MIN:
  804.           v = v1 < v2 ? v1 : v2;
  805.           break;
  806.           
  807.         case BINOP_MAX:
  808.           v = v1 > v2 ? v1 : v2;
  809.           break;
  810.           
  811.         default:
  812.           error ("Invalid binary operation on numbers.");
  813.         }
  814.       
  815.       val = allocate_value (BUILTIN_TYPE_LONGEST);
  816.       store_signed_integer (VALUE_CONTENTS_RAW (val),
  817.                 TYPE_LENGTH (VALUE_TYPE (val)),
  818.                 v);
  819.     }
  820.     }
  821.  
  822.   return val;
  823. }
  824.  
  825. /* Simulate the C operator ! -- return 1 if ARG1 contains zero.  */
  826.  
  827. int
  828. value_logical_not (arg1)
  829.      value arg1;
  830. {
  831.   register int len;
  832.   register char *p;
  833.  
  834.   COERCE_ARRAY (arg1);
  835.  
  836.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FLT)
  837.     return 0 == value_as_double (arg1);
  838.  
  839.   len = TYPE_LENGTH (VALUE_TYPE (arg1));
  840.   p = VALUE_CONTENTS (arg1);
  841.  
  842.   while (--len >= 0)
  843.     {
  844.       if (*p++)
  845.     break;
  846.     }
  847.  
  848.   return len < 0;
  849. }
  850.  
  851. /* Simulate the C operator == by returning a 1
  852.    iff ARG1 and ARG2 have equal contents.  */
  853.  
  854. int
  855. value_equal (arg1, arg2)
  856.      register value arg1, arg2;
  857.  
  858. {
  859.   register int len;
  860.   register char *p1, *p2;
  861.   enum type_code code1;
  862.   enum type_code code2;
  863.  
  864.   COERCE_ARRAY (arg1);
  865.   COERCE_ARRAY (arg2);
  866.  
  867.   code1 = TYPE_CODE (VALUE_TYPE (arg1));
  868.   code2 = TYPE_CODE (VALUE_TYPE (arg2));
  869.  
  870.   if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
  871.     return value_as_long (arg1) == value_as_long (arg2);
  872.   else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
  873.        && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
  874.     return value_as_double (arg1) == value_as_double (arg2);
  875.  
  876.   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  877.      is bigger.  */
  878.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
  879.     return value_as_pointer (arg1) == (CORE_ADDR) value_as_long (arg2);
  880.   else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
  881.     return (CORE_ADDR) value_as_long (arg1) == value_as_pointer (arg2);
  882.  
  883.   else if (code1 == code2
  884.        && ((len = TYPE_LENGTH (VALUE_TYPE (arg1)))
  885.            == TYPE_LENGTH (VALUE_TYPE (arg2))))
  886.     {
  887.       p1 = VALUE_CONTENTS (arg1);
  888.       p2 = VALUE_CONTENTS (arg2);
  889.       while (--len >= 0)
  890.     {
  891.       if (*p1++ != *p2++)
  892.         break;
  893.     }
  894.       return len < 0;
  895.     }
  896.   else
  897.     {
  898.       error ("Invalid type combination in equality test.");
  899.       return 0;  /* For lint -- never reached */
  900.     }
  901. }
  902.  
  903. /* Simulate the C operator < by returning 1
  904.    iff ARG1's contents are less than ARG2's.  */
  905.  
  906. int
  907. value_less (arg1, arg2)
  908.      register value arg1, arg2;
  909. {
  910.   register enum type_code code1;
  911.   register enum type_code code2;
  912.  
  913.   COERCE_ARRAY (arg1);
  914.   COERCE_ARRAY (arg2);
  915.  
  916.   code1 = TYPE_CODE (VALUE_TYPE (arg1));
  917.   code2 = TYPE_CODE (VALUE_TYPE (arg2));
  918.  
  919.   if (code1 == TYPE_CODE_INT && code2 == TYPE_CODE_INT)
  920.     {
  921.       if (TYPE_UNSIGNED (VALUE_TYPE (arg1))
  922.        || TYPE_UNSIGNED (VALUE_TYPE (arg2)))
  923.     return ((unsigned LONGEST) value_as_long (arg1)
  924.         < (unsigned LONGEST) value_as_long (arg2));
  925.       else
  926.     return value_as_long (arg1) < value_as_long (arg2);
  927.     }
  928.   else if ((code1 == TYPE_CODE_FLT || code1 == TYPE_CODE_INT)
  929.        && (code2 == TYPE_CODE_FLT || code2 == TYPE_CODE_INT))
  930.     return value_as_double (arg1) < value_as_double (arg2);
  931.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
  932.     return value_as_pointer (arg1) < value_as_pointer (arg2);
  933.  
  934.   /* FIXME: Need to promote to either CORE_ADDR or LONGEST, whichever
  935.      is bigger.  */
  936.   else if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_INT)
  937.     return value_as_pointer (arg1) < (CORE_ADDR) value_as_long (arg2);
  938.   else if (code2 == TYPE_CODE_PTR && code1 == TYPE_CODE_INT)
  939.     return (CORE_ADDR) value_as_long (arg1) < value_as_pointer (arg2);
  940.  
  941.   else
  942.     {
  943.       error ("Invalid type combination in ordering comparison.");
  944.       return 0;
  945.     }
  946. }
  947.  
  948. /* The unary operators - and ~.  Both free the argument ARG1.  */
  949.  
  950. value
  951. value_neg (arg1)
  952.      register value arg1;
  953. {
  954.   register struct type *type;
  955.  
  956.   COERCE_ENUM (arg1);
  957.  
  958.   type = VALUE_TYPE (arg1);
  959.  
  960.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  961.     return value_from_double (type, - value_as_double (arg1));
  962.   else if (TYPE_CODE (type) == TYPE_CODE_INT)
  963.     return value_from_longest (type, - value_as_long (arg1));
  964.   else {
  965.     error ("Argument to negate operation not a number.");
  966.     return 0;  /* For lint -- never reached */
  967.   }
  968. }
  969.  
  970. value
  971. value_complement (arg1)
  972.      register value arg1;
  973. {
  974.   COERCE_ENUM (arg1);
  975.  
  976.   if (TYPE_CODE (VALUE_TYPE (arg1)) != TYPE_CODE_INT)
  977.     error ("Argument to complement operation not an integer.");
  978.  
  979.   return value_from_longest (VALUE_TYPE (arg1), ~ value_as_long (arg1));
  980. }
  981.  
  982. /* The INDEX'th bit of SET value whose VALUE_TYPE is TYPE,
  983.    and whose VALUE_CONTENTS is valaddr.
  984.    Return -1 if out of range, -2 other error. */
  985.  
  986. int
  987. value_bit_index (type, valaddr, index)
  988.      struct type *type;
  989.      char *valaddr;
  990.      int index;
  991. {
  992.   struct type *range;
  993.   int low_bound, high_bound, bit_length;
  994.   LONGEST word;
  995.   range = TYPE_FIELD_TYPE (type, 0);
  996.   if (TYPE_CODE (range) != TYPE_CODE_RANGE)
  997.     return -2;
  998.   low_bound = TYPE_LOW_BOUND (range);
  999.   high_bound = TYPE_HIGH_BOUND (range);
  1000.   if (index < low_bound || index > high_bound)
  1001.     return -1;
  1002.   bit_length = high_bound - low_bound + 1;
  1003.   index -= low_bound;
  1004.   if (bit_length <= TARGET_CHAR_BIT)
  1005.     word = unpack_long (builtin_type_unsigned_char, valaddr);
  1006.   else if (bit_length <= TARGET_SHORT_BIT)
  1007.     word = unpack_long (builtin_type_unsigned_short, valaddr);
  1008.   else
  1009.     {
  1010.       int word_start_index = (index / TARGET_INT_BIT) * TARGET_INT_BIT;
  1011.       index -= word_start_index;
  1012.       word = unpack_long (builtin_type_unsigned_int,
  1013.               valaddr + (word_start_index / HOST_CHAR_BIT));
  1014.     }
  1015. #if BITS_BIG_ENDIAN
  1016.   if (bit_length <= TARGET_CHAR_BIT)
  1017.     index = TARGET_CHAR_BIT - 1 - index;
  1018.   else if (bit_length <= TARGET_SHORT_BIT)
  1019.     index = TARGET_SHORT_BIT - 1 - index;
  1020.   else
  1021.     index = TARGET_INT_BIT - 1 - index;
  1022. #endif
  1023.   return (word >> index) & 1;
  1024. }
  1025.  
  1026. value
  1027. value_in (element, set)
  1028.      value element, set;
  1029. {
  1030.   int member;
  1031.   if (TYPE_CODE (VALUE_TYPE (set)) != TYPE_CODE_SET)
  1032.     error ("Second argument of 'IN' has wrong type");
  1033.   if (TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_INT
  1034.       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_CHAR
  1035.       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_ENUM
  1036.       && TYPE_CODE (VALUE_TYPE (element)) != TYPE_CODE_BOOL)
  1037.     error ("First argument of 'IN' has wrong type");
  1038.   member = value_bit_index (VALUE_TYPE (set), VALUE_CONTENTS (set),
  1039.                 value_as_long (element));
  1040.   if (member < 0)
  1041.     error ("First argument of 'IN' not in range");
  1042.   return value_from_longest (builtin_type_int, member);
  1043. }
  1044.